#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>

#include "testopencv.h"

#include <iostream>
using namespace cv;
using namespace std;

uint8_t exitFlag = ON;

uint8_t exitVideo(uint8_t flag){
    exitFlag = flag;
}

void *opencvVideo(void *ptr)
{
    // 视频保存位置
    string outputVideoPath = "./test.avi";  
 
    // 打开摄像头
    VideoCapture capture0(0);  
 
    VideoWriter outputVideo;
    
    // 获取摄像机帧率
    int fps = capture0.get(CAP_PROP_FPS);  
 
    // 获取当前摄像头的视频信息
    cv::Size S = cv::Size((int)capture0.get(CAP_PROP_FRAME_WIDTH),
                          (int)capture0.get(CAP_PROP_FRAME_HEIGHT));
    // 打开视频路径，设置基本信息 open函数中你参数跟上面给出的VideoWriter函数是一样的
    outputVideo.open(outputVideoPath, cv::VideoWriter::fourcc('X', 'V', 'I', 'D'), fps, S, true);
 
    if (!outputVideo.isOpened()) {
        cout << "fail to open!" << endl;
        return 0;
    }
 
    // 图片帧
    cv::Mat frameImage;
    int count = 0;
 
    while(true){
        // 读取当前帧
        capture0 >> frameImage;
        if(frameImage.empty()) break;
        ++count;
        // 输出当前帧
        //cv::imshow("output", frameImage);
        // 保存当前帧
        outputVideo << frameImage;
        //if (char(waitKey(1)) == 'q') break;
        if ( exitFlag == OFF) break;
    }
	return 0;
}